import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
  OneToMany,
  Unique
} from 'typeorm';
import { DeviceGroup } from './DeviceGroup';
import { User } from './User';
import { SceneAction } from './SceneAction';

@Entity()
@Unique(['gid', 'name']) // 同一分组下 name 唯一
export class Scene {
  @PrimaryGeneratedColumn('uuid')
  scene_id!: string;

  @Column({ type: 'varchar', length: 255 })
  userId!: string;

  @Column()
  gid!: string;

  @Column()
  name!: string;

  // 分组关系
  @ManyToOne(() => DeviceGroup, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'gid' })
  group!: DeviceGroup;

  // 用户关系
  @ManyToOne(() => User, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'userId' })
  user!: User;

  @OneToMany(() => SceneAction, action => action.scene, { cascade: true })
  actions!: SceneAction[];
}
